//
//  JYJMusicListTableViewCell.m
//  HappyListen
//
//  Created by 冀永金 on 16/8/30.
//  Copyright © 2016年 冀永金. All rights reserved.
//

#import "JYJMusicListTableViewCell.h"
#import "BaseLabel.h"
#import "Masonry.h"
#import "UIImageView+WebCache.h"
#import "BaseCollectionView.h"
#import "JYJTAMacro.h"
#import "JYJChannelModel.h"
#import "JYJBestMusicListCollectionViewCell.h"
#import "BaseButton.h"
#import "JYJBestMuscicListModel.h"
#import "JYJMusicsModel.h"
#import "JYJMusicsContentModel.h"
#import "JYJChannelModel.h"
#import "JYJVCOfSortContent.h"
#import "JYJMusicPlayerViewController.h"
#import "JYJVCOfFeaturedTopThreeDetails.h"

@interface JYJMusicListTableViewCell ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) UIImageView *image;
@property (nonatomic, strong) BaseLabel *nameLabel;
@property (nonatomic, strong) UIImageView *itemImage;
@property (nonatomic, strong) BaseButton *buttonOfMore;
@property (nonatomic, strong) BaseCollectionView *collection;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@property (nonatomic, strong) NSMutableArray *modelArr;
@end

@implementation JYJMusicListTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self configViews];
        
        
    }
    return self;
}
- (void)configViews {
    self.image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"find_cat_header"]];
    [self.contentView addSubview:_image];
    self.nameLabel = [[BaseLabel alloc]init];
    self.nameLabel.textColor = [UIColor whiteColor];
    [self.contentView addSubview:_nameLabel];
    self.itemImage = [[UIImageView alloc]init];
    [self.contentView addSubview:_itemImage];
    self.buttonOfMore = [BaseButton buttonWithType:UIButtonTypeSystem];
    [self.contentView addSubview:_buttonOfMore];
    [_buttonOfMore setTitle:@"更多" forState:UIControlStateNormal];
    [_buttonOfMore setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [_buttonOfMore addTarget:self action:@selector(handleMore:) forControlEvents:UIControlEventTouchUpInside];
    _layout = [[UICollectionViewFlowLayout alloc]init];
    self.collection = [[BaseCollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:_layout];
    [self.contentView addSubview:_collection];
    self.collection.delegate = self;
    self.collection.dataSource = self;
    [self.collection registerClass:[JYJBestMusicListCollectionViewCell class] forCellWithReuseIdentifier:@"MusicList"];
    self.collection.scrollEnabled = NO;

}
- (void)setMusicModel:(JYJMusicsModel *)musicModel {
    _musicModel = musicModel;
    [self.collection reloadData];
}
- (void)layoutSubviews {
    [super layoutSubviews];
    _layout.itemSize = CGSizeMake((SCREEN_WIDTH - 50) / 4, (SCREEN_WIDTH - 50) / 2.5);
    _layout.sectionInset = UIEdgeInsetsMake(10, 10, 5, 10);
    _layout.minimumLineSpacing = 10;
  self.collection.frame = CGRectMake(0, 50, SCREEN_WIDTH, self.bounds.size.height - 50);
    [self.image mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top).with.offset(10);
        make.left.equalTo(self.contentView.mas_left).with.offset(10);
        make.width.mas_equalTo(35);
        make.height.mas_equalTo(35);
    }];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top).with.offset(13);
        make.left.equalTo(self.image.mas_right).with.offset(5);
        make.width.mas_equalTo (100);
        make.height.mas_equalTo(30);
    }];
    [self.itemImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.image.mas_bottom).with.offset(10);
        make.left.equalTo(self.contentView.mas_left).with.offset(10);
        make.bottom.equalTo(self.contentView.mas_bottom).with.offset(-10);
        make.right.equalTo(self.contentView.mas_right).with.offset(-10);
    }];
    [self.buttonOfMore mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top).with.offset(18);
        make.right.equalTo(self.contentView.mas_right).with.offset(-10);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(25);
    }];

}
- (void)setChanneModel:(JYJChannelModel *)channeModel {
    _channeModel = channeModel;
    self.nameLabel.text = channeModel.channel_name;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _musicModel.musics.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    JYJBestMusicListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MusicList" forIndexPath:indexPath];
    JYJMusicsContentModel *model = _musicModel.musics[indexPath.row];
    [cell setMusicContentModel:model];
    return cell;
}
- (void)handleMore:(BaseButton *)button {
    JYJVCOfSortContent *sortContent = [[JYJVCOfSortContent alloc]init];
    sortContent.channelModel = self.channeModel;
    self.handleAction(sortContent);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    JYJVCOfFeaturedTopThreeDetails *detailsVC = [[JYJVCOfFeaturedTopThreeDetails alloc]init];
    JYJMusicsContentModel *model = _musicModel.musics[indexPath.row];
    detailsVC.musicsContentModel = model;
    self.handleVC(detailsVC);

}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
